package com.begye.smartloan.apptest.sezhi

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtSeZhiTouXiangBinding
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.sezhi.adapter.SeZhiTouXiangAdapter
import com.begye.smartloan.apptest.tool.SpTool
import com.begye.smartloan.apptest.tool.addTouch
import org.json.JSONObject


@SuppressLint("CustomWelcomeScreen")
class SeZhiTouXiangActivity : AppCompatActivity() {

    companion object {
        const val YONG_HU_TOU_XIANG = "YONG_HU_TOU_XIANG"
    }

    private lateinit var _binding: AtSeZhiTouXiangBinding
    private lateinit var _seZhiTouXiangAdapter: SeZhiTouXiangAdapter

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtSeZhiTouXiangBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configUIEvent()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun configUIEvent() {

        _binding.atSeZhiTouXiangNar.initConfig(
            text = getString(R.string.se_zhi_text_3),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                finish()
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        var local = SpTool.get(YONG_HU_TOU_XIANG)
        if (local.isBlank()) {
            local = "0"
        }

        val list = mutableListOf<JSONObject>()
        list.add(JSONObject().apply {
            put("isChecked", local == "0")
            put("resId", R.drawable.dres_119)
        })
        list.add(JSONObject().apply {
            put("isChecked", local == "1")
            put("resId", R.drawable.dres_120)
        })
        list.add(JSONObject().apply {
            put("isChecked", local == "2")
            put("resId", R.drawable.dres_121)
        })
        list.add(JSONObject().apply {
            put("isChecked", local == "3")
            put("resId", R.drawable.dres_122)
        })
        list.add(JSONObject().apply {
            put("isChecked", local == "4")
            put("resId", R.drawable.dres_123)
        })
        list.add(JSONObject().apply {
            put("isChecked", local == "5")
            put("resId", R.drawable.dres_124)
        })

        _seZhiTouXiangAdapter = SeZhiTouXiangAdapter(list) {
            _seZhiTouXiangAdapter.notifyDataSetChanged()
            SpTool.save(YONG_HU_TOU_XIANG, it.toString())
        }

        _binding.atSeZhiTouXiangList.layoutManager = GridLayoutManager(this, 3)
        _binding.atSeZhiTouXiangList.adapter = _seZhiTouXiangAdapter
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

